package com.itheima.StudentSystem;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class userTest {
    public static void main(String[] args) {
        String pin = pinCard();//接收验证码
        ArrayList<user> list = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        while(true){
            System.out.println("欢迎来到学生管理系统");
            System.out.println("请选择操作1登录 2注册 3忘记密码 4.退出");
            String num = sc.next();
            switch (num){
                case "1":
                    loginUser(list,pin);//登录
                    break;
                case "2":
                    registerUser(list);//注册
                    break;
                case "3":
                    forgetPassword(list);//忘记密码
                    break;
                case "4":
                    return;
                default:
                    System.out.println("没有这个选项，请重新输入");
            }
        }
    }

    public static void registerUser(ArrayList<user> list){
        //注册
        user u1 = new user();
        Scanner sc = new Scanner(System.in);
        boolean nameok = true;

        loop1:while (nameok) {
            System.out.println("请输入用户名");
            String name = sc.next();
            //判断用户名是否唯一
            if(checkIdcard(list, name)>=0){
                System.out.println("用户名已经存在");
            }else if(name.length()<3||name.length()>15){
                System.out.println("用户名长度必须在3~15位之间 ");
            }else {
                for (int i = 0; i < name.length(); i++) {
                    char c = name.charAt(i);
                    if((c>='a'&&c<='z')||(c>='A'&&c<='Z')){
                        System.out.println("用户名输入成功");
                        u1.setIdName(name);
                        nameok=false;
                        break loop1;
                    }
                }
                System.out.println("只能是字母加数字的组合，但是不能是纯数字");
            }

        }
        //密码键盘输入两次，两次一致才可以进行注册。
        while(true){
            System.out.println("请输入密码");
            String password1 = sc.next();
            System.out.println("请确认密码");
            String password2 = sc.next();
            boolean equals = password1.equals(password2);
            if(equals){
                System.out.println("输入成功");
                u1.setPassWord(password1);
                break;
            }else{
                System.out.println("两次密码不匹配,请重新输入");
            }
        }
        //长度为18位
        //不能以0为开头
        //前17位，必须都是数字
        //最后一位可以是数字，也可以是大写X或小写x
        boolean idcardok = true;
        loop2:while(idcardok){
            System.out.println("请输入身份证");
            String idcard = sc.next();
            int length = idcard.length();
            if(length==18){
                if(idcard.charAt(0)!='0'){
                    int truenum=0;
                    for (int i = 0; i < length-1; i++) {
                        char c = idcard.charAt(i);
                        if(c>'0'&&c<'9') truenum++;
                    }
                    if(truenum==17){
                        char c = idcard.charAt(idcard.length() - 1);
                        if(c=='x'||c=='X'||(c>'0'&&c<'9')){
                            System.out.println("输入成功");
                            u1.setIdCard(idcard);
                            idcardok=false;
                            break;
                        }else {
                            System.out.println("最后一位是数字，也可以是大写X或小写x");
                        }
                    }else {
                        System.out.println("前17位，必须都是数字，请重新输入");
                    }

                }else {
                    System.out.println("不能以0为开头,请重新输入");
                }
            }else {
                System.out.println("长度为18位,请重新输入");
            }
        }
        //长度为11位
        //不能以0为开头
        //必须都是数字
        boolean phonenumberok = true;
        loop3:while (phonenumberok){
            System.out.println("请输入手机号码");
            String phonenumber = sc.next();
            if(phonenumber.length()==11){
                if(phonenumber.charAt(0)!='0'){
                    boolean conutinue = true;
                    for (int i = 0; i < phonenumber.length(); i++) {
                        if(phonenumber.charAt(i)<'0'||phonenumber.charAt(i)>'9'){
                            System.out.println("验证失败，必须都是数字");
                            conutinue=false;
                            break;
                        }
                    }
                    if(conutinue){
                        System.out.println("验证成功");
                        u1.setPhoneNumber(phonenumber);
                        phonenumberok=false;
                    }
                }else {
                    System.out.println("验证失败，不能以0为开头");
                }
            }else {
                System.out.println("验证失败，长度为11位");
            }
        }
        System.out.println("账户注册成功");
        System.out.println("您的用户名为"+u1.getIdName());
        System.out.println("您的密码为"+u1.getPassWord());
        System.out.println("您的身份证为"+u1.getIdCard());
        System.out.println("您的手机号为"+u1.getPhoneNumber());
        list.add(u1);


    }

    public static void loginUser(ArrayList<user> list,String pin){
        //登录
        Scanner sc = new Scanner(System.in);

        System.out.println("请输入用户名");
        String name = sc.next();
        //判断用户名是否注册
        int i = checkIdcard(list, name);
        if (i>=0) {
            while (true){
                System.out.println("请输入验证码" + pin);
                String pincode = sc.next();
                //判断验证码是否正确
                if (pincode.equals(pin)) {
                    for (int j = 0; j < 3; j++) {
                        //三次机会
                        System.out.println("请输入密码");
                        String password = sc.next();
                        System.out.println("请输入用户名");
                        String name1 = sc.next();
                        if ((list.get(i).getPassWord().equals(password)) && (list.get(i).getIdName().equals(name1))) {
                            System.out.println("登录成功");
                            return;
                        } else {
                            System.out.println("登录失败，还有" + (3 - j - 1) + "次机会");
                        }
                    }
                    return;
                }else {
                    System.out.println("验证码错误,请重新输入");
                }
            }
        }else {
            //用户未注册
            System.out.println("用户名未注册，请先注册");
            return;
        }

    }

    public static void forgetPassword(ArrayList<user> list){
        //忘记密码
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名");
        String name = sc.next();
        //判断用户名是否注册
        int i = checkIdcard(list, name);
        if (i>=0) {
            System.out.println("请输入身份证");
            String idcard = sc.next();
            System.out.println("请输入手机号码");
            String phonenumber = sc.next();
            if(idcard.equals(list.get(i).getIdCard())&&phonenumber.equals(list.get(i).getPhoneNumber())){
                System.out.println("验证成功");
                System.out.println("请输入新密码");
                String newpassword = sc.next();
                System.out.println("请确认新密码");
                String newpassword2 = sc.next();
                if(newpassword.equals(newpassword2)){
                    list.get(i).setPassWord(newpassword);
                    System.out.println("修改成功,新密码为"+newpassword);
                }
            }else {
                System.out.println("验证失败,账号信息不匹配");
            }
        }else {
            //用户未注册
            System.out.println("用户名未注册，请先注册");
            return;
        }

    }

    public static String pinCard(){
        //验证码
        String AZ = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        String num="1234567890";
        String result="";
        char[] A = AZ.toCharArray();
        char[] B = num.toCharArray();
        Random random = new Random();
        for (int i = 0; i < 4; i++) {
            char c = A[random.nextInt(A.length)];
            result=result+c;
        }
        char c = B[random.nextInt(B.length)];
        result=result+c;
        return result;
    }

    public static int checkIdcard(ArrayList<user> list,String id){
        //判断用户名是否已经存在
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getIdName().equals(id)){
                return i;
            }
        }
        return -1;
    }


}
